<!-- templates/index.html -->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>医院智能问诊分类系统</title>
    <style>
        :root {
            --hospital-blue: #007bff;
            --hospital-green: #28a745;
            --hospital-gray: #6c757d;
            --hospital-light: #f8f9fa;
            --hospital-border: #dee2e6;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Microsoft YaHei', Arial, sans-serif;
        }

        body {
            background-color: #f5f7fa;
            color: #333;
            line-height: 1.6;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        header {
            background: linear-gradient(135deg, var(--hospital-blue), #0056b3);
            color: white;
            padding: 20px;
            text-align: center;
            border-radius: 8px 8px 0 0;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        .logo {
            font-size: 24px;
            font-weight: bold;
            margin-bottom: 10px;
        }

        .subtitle {
            font-size: 16px;
            opacity: 0.9;
        }

        .main-content {
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 15px rgba(0,0,0,0.1);
            margin-top: 20px;
            overflow: hidden;
        }

        .input-section {
            padding: 30px;
            border-bottom: 1px solid var(--hospital-border);
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #495057;
        }

        textarea {
            width: 100%;
            min-height: 120px;
            padding: 15px;
            border: 2px solid var(--hospital-border);
            border-radius: 6px;
            font-size: 16px;
            resize: vertical;
            transition: border-color 0.3s;
        }

        textarea:focus {
            outline: none;
            border-color: var(--hospital-blue);
            box-shadow: 0 0 0 3px rgba(0,123,255,0.25);
        }

        .btn {
            background-color: var(--hospital-blue);
            color: white;
            border: none;
            padding: 12px 30px;
            font-size: 16px;
            border-radius: 6px;
            cursor: pointer;
            transition: background-color 0.3s;
            font-weight: 600;
        }

        .btn:hover {
            background-color: #0056b3;
        }

        .btn:disabled {
            background-color: var(--hospital-gray);
            cursor: not-allowed;
        }

        .result-section {
            padding: 30px;
            display: none;
        }

        .result-title {
            font-size: 20px;
            margin-bottom: 20px;
            color: var(--hospital-blue);
            text-align: center;
        }

        .result-card {
            background-color: var(--hospital-light);
            border-left: 4px solid var(--hospital-green);
            padding: 20px;
            border-radius: 6px;
            margin-bottom: 20px;
        }

        .result-item {
            margin-bottom: 15px;
            padding-bottom: 15px;
            border-bottom: 1px dashed var(--hospital-border);
        }

        .result-item:last-child {
            margin-bottom: 0;
            padding-bottom: 0;
            border-bottom: none;
        }

        .item-label {
            font-weight: 600;
            color: #495057;
            margin-bottom: 5px;
        }

        .item-value {
            font-size: 18px;
            color: #212529;
        }

        .response-time {
            color: var(--hospital-green);
            font-weight: 600;
        }

        .loading {
            text-align: center;
            padding: 20px;
            display: none;
        }

        .spinner {
            border: 4px solid rgba(0,0,0,0.1);
            border-radius: 50%;
            border-top: 4px solid var(--hospital-blue);
            width: 40px;
            height: 40px;
            animation: spin 1s linear infinite;
            margin: 0 auto 15px;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        footer {
            text-align: center;
            margin-top: 30px;
            color: var(--hospital-gray);
            font-size: 14px;
        }

        .error-message {
            color: #dc3545;
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            padding: 15px;
            border-radius: 6px;
            margin-top: 20px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <div class="logo">🏥 医院智能问诊分类系统</div>
            <div class="subtitle">基于FastText的医疗文本智能分类平台</div>
        </header>

        <div class="main-content">
            <div class="input-section">
                <div class="form-group">
                    <label for="medical-text">请输入患者咨询内容：</label>
                    <textarea id="medical-text" placeholder="例如：中药调理月经不调有效果吗？"></textarea>
                </div>
                <button id="predict-btn" class="btn">开始分析</button>

                <div class="loading" id="loading">
                    <div class="spinner"></div>
                    <p>正在分析中，请稍候...</p>
                </div>

                <div class="error-message" id="error-message"></div>
            </div>

            <div class="result-section" id="result-section">
                <h2 class="result-title">分析结果</h2>
                <div class="result-card">
                    <div class="result-item">
                        <div class="item-label">原始输入：</div>
                        <div class="item-value" id="input-text"></div>
                    </div>
                    <div class="result-item">
                        <div class="item-label">预测分类：</div>
                        <div class="item-value" id="prediction-result"></div>
                    </div>
                    <div class="result-item">
                        <div class="item-label">响应时间：</div>
                        <div class="item-value"><span class="response-time" id="response-time"></span> 毫秒</div>
                    </div>
                </div>
            </div>
        </div>

        <footer>
            <p>© 2023 医院智能问诊分类系统 | 基于FastText深度学习模型</p>
        </footer>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const predictBtn = document.getElementById('predict-btn');
            const medicalText = document.getElementById('medical-text');
            const loading = document.getElementById('loading');
            const resultSection = document.getElementById('result-section');
            const errorMessage = document.getElementById('error-message');

            // 结果显示元素
            const inputText = document.getElementById('input-text');
            const predictionResult = document.getElementById('prediction-result');
            const responseTime = document.getElementById('response-time');

            predictBtn.addEventListener('click', async function() {
                const text = medicalText.value.trim();

                if (!text) {
                    showError('请输入患者咨询内容');
                    return;
                }

                // 显示加载状态
                showLoading(true);
                hideError();
                resultSection.style.display = 'none';

                try {
                    const response = await fetch('/predict', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({text: text})
                    });

                    const data = await response.json();

                    if (response.ok) {
                        // 显示结果
                        inputText.textContent = data.text;
                        predictionResult.textContent = data.pred_class;
                        responseTime.textContent = data.response_time;

                        resultSection.style.display = 'block';
                    } else {
                        showError(data.error || '分析失败，请重试');
                    }
                } catch (error) {
                    showError('网络错误，请检查连接后重试');
                    console.error('Error:', error);
                } finally {
                    showLoading(false);
                }
            });

            function showLoading(show) {
                loading.style.display = show ? 'block' : 'none';
                predictBtn.disabled = show;
            }

            function showError(message) {
                errorMessage.textContent = message;
                errorMessage.style.display = 'block';
            }

            function hideError() {
                errorMessage.style.display = 'none';
            }
        });
    </script>
</body>
</html>
